/**
 * Created by zhaojm on 15/3/29.
 */

game.SparkEffect = cc.Class.extend({

    //_sprite:null,
    _one : null,
    _two : null,
    _three : null,

    ctor:function(){

        this._one = new cc.Sprite(res.explode1_jpg);
        this._two = new cc.Sprite(res.explode2_jpg);
        this._three = new cc.Sprite(res.explode3_jpg);

        //this._one.setBlendFunc(gl.SRC_ALPHA, gl.ONE);
        //this._two.setBlendFunc(gl.SRC_ALPHA, gl.ONE);
        //this._three.setBlendFunc(gl.SRC_ALPHA, gl.ONE);

        this._three.setRotation(Math.random() * 360);
    },

    addToLayer : function(layer, pos, scale, duration){
        this._one.setPosition(pos);
        this._two.setPosition(pos);
        this._three.setPosition(pos);

        layer.addChild(this._one);
        layer.addChild(this._two);
        layer.addChild(this._three);

        this._one.setScale(scale);
        this._two.setScale(scale);
        this._three.setScale(scale);

        this._runAction(duration);
    },

    _runAction:function(duration){
        var left = cc.RotateBy.create(duration, -45);
        var right = cc.RotateBy.create(duration, 45);
        var scaleBy = cc.ScaleBy.create(duration, 3);
        var fadeOut = cc.FadeOut.create(duration);

        var removeOne =  cc.CallFunc.create(this.removeFromLayer);
        var removeTwo =  cc.CallFunc.create(this.removeFromLayer);
        var removeThree =  cc.CallFunc.create(this.removeFromLayer);
        var seqOne = cc.Sequence.create(fadeOut, removeOne);
        var seqTwo = cc.Sequence.create(fadeOut.clone(), removeTwo);
        var seqThree = cc.Sequence.create(fadeOut.clone(), removeThree);

        this._one.runAction(left);
        this._two.runAction(right);

        this._one.runAction(scaleBy);
        this._two.runAction(scaleBy.clone());
        this._three.runAction(scaleBy.clone());

        this._one.runAction(seqOne);
        this._two.runAction(seqTwo);
        this._three.runAction(seqThree);
    },

    removeFromLayer : function(pSender){
        pSender.removeFromParent();
    },


});